package com.zhongzhi.javafx.controller;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.commons.math3.stat.descriptive.moment.Mean;

import com.zhongzhi.common.Common;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.StationShow;
import com.zhongzhi.springboot.pojo.Type;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
@Log4j
public class OpenRunController implements Initializable{
	@FXML
	private MenuButton station;
	@FXML
	private TextField hoig;
	@FXML
	private Button stop;
	@FXML
	private Button run;
    @FXML
    private MenuButton type;
	
	private String typeName;
	private String stationName;
	
	private Integer typeId;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		log.info("启动运行窗口:开始初始化");
		initselect();
		typeId=Common.getSelectStation();
		
		stop.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub
				Stage window = (Stage) stop.getScene().getWindow();
				window.close();
			}
		});
		
		run.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				try {
					if (hoig.getText()!=null && hoig.getText().equals("")!=true) {
						String string = "http://127.0.0.1:8080/doosanRadar/ds/startdetection?kmatn="+typeName
								+"&hogi="+hoig.getText().trim()
								+"&station="+stationName
								+"&size=6"
								+"&upa=0.0"
								+"&upb=0.0"
								+"&downa=0.0"
								+"&downb=0.0";
						log.info("启动运行窗口:启动任务执行:"+string);
						String gethttp = ClientUtil.Gethttp(string);
						log.info("启动运行窗口:启动任务执行结果:"+gethttp);
						Stage window = (Stage) stop.getScene().getWindow();
						window.close();
					}
				} catch (Exception e) {
					log.error("启动运行窗口:启动任务执行失败:"+e);
				}
				
			}
		});
		log.info("启动运行窗口:初始化完毕");
		
	}
	private void initselect() {
		// TODO Auto-generated method stub
		//初始化查询机型
		try {
			String strings = "http://127.0.0.1:8080/type/select?pages="+1+"&line="+200000;
			log.info("启动运行窗口:查询机型:"+strings);
			String gethttp = ClientUtil.Gethttp(strings);
			log.info("启动运行窗口:查询机型结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			String string = jsonobj.getData().toString();
			List<Type> jsonlist = ClientUtil.Jsonlist(string, Type.class);
			for (Type typea : jsonlist) {
				MenuItem item = new MenuItem(typea.getTypeName());
				item.setOnAction(new EventHandler<ActionEvent>() {
					
					@Override
					public void handle(ActionEvent event) {
						type.setText(typea.getTypeName());
						typeName=typea.getTypeName();
					}
				});
				type.getItems().add(item);
			}
			HashMap<Integer, Station> taskStationMap = Common.getTaskStationMap();
			Set<Integer> keySet = taskStationMap.keySet();
			for (Integer integer : keySet) {
				Station station2 = taskStationMap.get(integer);
				
				MenuItem item = new MenuItem(station2.getStationname());
				item.setOnAction(new EventHandler<ActionEvent>() {
					
					@Override
					public void handle(ActionEvent event) {
						station.setText(station2.getStationname());
						stationName=station2.getStationname();
					}
				});
				station.getItems().add(item);
				
			}
		} catch (Exception e) {
			log.error("启动运行窗口:查询机型失败:"+e);
		}
		
		
		
	}
	
	
}
